const Actions = require('./actions');
const DB = require('app/db.js');
import { Storage } from 'clientConfig/util/StoreData'
module.exports = Reflux.createStore({
    listenables: [Actions],
    data: {

    },
    onFetchAwardList: function (pageNum, props, success, error) {
        let t = this;
        DB.Teacher.get_award_lists({
            schoolId: Storage.get('user.schoolId')||'f20171120',
            teacherId: Storage.get('user.teacherId')||'4729',
            pageIndex: pageNum,
            classId: props.classId
        })
            .then((content) => {
                let schoolId = content.check.schoolId;
                let classId = content.check.classId;
                let teacherId = content.check.teacherId;
                content = content.data.list;
                t.data.classId = props.classId;
                var list = [];
                var obj = {};
                if (content && content.length > 0) {
                    for (let item of content) {
                        obj["schoolId"] = schoolId;
                        obj["teacherId"] = item.teacherId;
                        obj["badge"] = false;
                        obj["title"] = item.title;
                        obj["subtitle"] = item.time;
                        obj["awardId"] = item.id;
                        obj["content"] = item.remark;
                        obj["awardType"] = item.awardType;
                        obj["awardLevel"] = item.awardLevel;
                        obj["issuedCompany"] = item.issuedCompany;
                        obj["studentName"] = item.studentName;
                        obj["awardRank"] = item.awardRank;
                        obj["teacherName"] = item.teacherName;
                        obj["msg"] = item.msg;
                        list.push(obj);
                        obj = {};
                    }
                }
                const hasNoMore = list.length < props.pageSize;
                t.data.listData = list;
                if (success)
                    success(hasNoMore, list);
            })
            .catch(function (error) {

            });
    },
    onGetClassList: function (params, cb) {
        let t = this;
        DB.Teacher.get_class_lists({
            schoolId: Storage.get('user.schoolId')||'f20171120',
            teacherId: Storage.get('user.teacherId')||'4729'
        })
            .then((content) => {
                if (content.code == 1) {
                    let schoolId = content.check.schoolId;
                    let teacherId = content.check.teacherId;
                    content = content.data.list;
                    var list = [];
                    var obj = {};
                    if (content && content.length > 0) {
                        for (let item of content) {
                            obj.imgUrl = '';
                            obj.classId = item.id;
                            obj.title = item.name;
                            list.push(obj);
                            obj = {};
                        }
                    }
                    t.data.listdata = list;
                    if(!t.data.classId){
                        t.data.classId = list[0].classId;
                    } 
                    t.updateComponent();
                }
            })
            .catch(function (error) {

            });
    },
    updateComponent: function () {
        this.trigger(this.data);
    }
});
